|
|
|
|
|
|
|
Dim dtYesterday As Date
dtYesterday = DateAdd("d",-1,Now) |
|
|
|
|
|
|
|
|
The code simply tells the DateAdd function to add an interval of minus one days to the current system date. The same codes (d, w, m, and so on) you use with the Format function can be used to specify an interval type. |
|
|
|
|
|
|
|
|
Whereas DateAdd allows you to add an interval to a date, DateDiff allows you to determine the interval between two dates. This capability is useful in scheduling programs, for example, running a process every hour, like this one: |
|
|
|
|
|
|
|
|
Dim dtLastRun As Date
Dim dtNextRun As Date
Dim nMinRemain As Integer
dtLastRun = Now
dtNextRun = DateAdd("n", 60, Now)
nMinRemain = DateDiff("n", Now, dtNextRun)
While nMinRemain > 0
Debug.Print "Start in " & nMinRemain & " minutes."
nMinRemain = DateDiff("n", Now, dtNextRun)
DoEvents
Wend
MsgBox "OK Done waiting!" |
|
|
|
|
|
|
|
|
The While loop in the preceding code example causes the loop to continue as long as the interval between the current time and the next scheduled time is greater than zero. |
|
|
|
|
|
|
|
|
Much has been said in the news media about ''the year 2000 problem" and how it will wreak havoc on computer systems. The problem is that many programs (and people) use two digits to represent the year, which could cause confusion. Does 2/18/00 mean 2/18/2000 or 2/18/1900? This question has no simple solution. You can't assume that 2/18/00 refers to February 18, 2000 if it really represents the birthday of a 100-year-old person. |
|
|
|
|
|
|
|
|
In addition, many programs have files and calculations based on a two-digit year. Consider the following lines from a database table: |
|
|
|
|
|
|
|
|
Date amount
960101 123.45
960102 432.34
.
.
991231 442.00 |
|
|
|
|
|
|
|
|
In this file, dates are stored as integer numbers, making it easy to find a certain range of numbers. For example, to sum all amounts from 1997 forward, select rows in which the date is greater than or equal to the number 970101. However, suppose a date following the year 2000 is added: 000101. In this case, the program would fail. |
|
|
|
|
|
|
|
|
Luckily, most of these problems can be taken care of fairly easily in the PC world. The issues in Visual Basic will probably not be with dates stored in a database, but rather with dates being entered by users who still in some cases prefer the two-digit year. |
|
|
|
|
|